package com.example.impl;

import com.example.mapper.WxbCustomerMapper;
import com.example.model.UserInfo;
import com.example.model.WxbCustomer;
import com.example.util.WxbCustomerService;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
public class WxbCustomerServiceImpl implements WxbCustomerService {

    @Autowired
    private WxbCustomerMapper wxbCustomerMapper;


    @Override
    public void save(WxbCustomer wxbCustomer){
        if(StringUtils.isNotBlank(wxbCustomer.getCustomerId())){
            wxbCustomer.setCreatetime(new Date());
            wxbCustomer.setLoginPwd(secretShiroPass(wxbCustomer.getLoginName(),wxbCustomer.getLoginPwd()));
            wxbCustomerMapper.updateByPrimaryKeySelective(wxbCustomer);
        }else{
//            wxbCustomer.setCustomerId(UUID.randomUUID().toString().replaceAll("-", ""));
            wxbCustomer.setCustomerId(RandomStringUtils.randomAlphanumeric(8));
            wxbCustomer.setCreatetime(new Date());
            wxbCustomer.setLoginPwd(secretShiroPass(wxbCustomer.getLoginName(),wxbCustomer.getLoginPwd()));
            wxbCustomerMapper.insertSelective(wxbCustomer);
        }

    }


    @Override
    public WxbCustomer findById(String customerId) {

        if(StringUtils.isNotBlank(customerId)){
            WxbCustomer wxbCustomers=  wxbCustomerMapper.selectByPrimaryKey(customerId);
            return wxbCustomers;
        }else{
            return new WxbCustomer();
        }

    }

    @Override
    public List<WxbCustomer> findAll(String loginName) {

        if (StringUtils.isNotBlank(loginName)) {
            Example example = new Example(WxbCustomer.class);
            example.createCriteria().andLike("loginName", "%" + loginName + "%");
            return wxbCustomerMapper.selectByExample(example);
        }
        return wxbCustomerMapper.selectAll();
    }

    @Override
    public void delete(List<String> customerId) {
        Example example = new Example(WxbCustomer.class);
        example.createCriteria().andEqualTo("customerId",customerId);
        wxbCustomerMapper.deleteByExample(example);
    }

    @Override
    public WxbCustomer findByLoginName(String loginName) {

            Example example = new Example(WxbCustomer.class);
            example.and().andEqualTo("loginName",loginName);
            return wxbCustomerMapper.selectOneByExample(example);

    }

    private String secretShiroPass(String saltStr, String loginPwd) {
        String hashAlgorithmName = "MD5";//加密方式
        ByteSource salt = ByteSource.Util.bytes(saltStr);//以账号作为盐值

        int hashIterations = 56;// 加密56次（注意这里配置的值要与shiro的配置文件中配置的值保持一致。否则无法解密

        SimpleHash hash = new SimpleHash(hashAlgorithmName, loginPwd, salt, hashIterations);
        return hash.toString();
    }
}
